<template>
  <div>
    <div class="slider" ref="slider">
      <div class="slider-group" ref="sliderGroup">
        <slot></slot>
      </div>
      <div class="dots">
        <span class="dot" :class="{active:currentPageIndex===index}" v-for="(item,index) in dots" :key="index"></span>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
import { addClass } from "common/js/dom";

export default {
  name: "Slider",
  data(){
    return {
      dots:[],
      currentPageIndex:0
    }
  },
  props: {
    probeType: {
      type: Number,
      default: 1
    },
    loop: {
      type: Boolean,
      default: true
    },
    autoPlay: {
      type: Boolean,
      default: true
    },
    interval: {
      type: Number,
      default: 4000
    },
    click: {
      type: Boolean,
      default: true
    },
    threshold: {
      type: Number,
      default: 0.3
    },
    speed: {
      type: Number,
      default: 400
    }
  },
  mounted() {
    if (this.slider) {
      this.slider.destroy()
    }
    this.$nextTick(() => {
      this.init();  
    })

    //视口大小改变时触发
    window.addEventListener('resize', () => {   
      if (!this.slider) {
        return;
      }
      clearTimeout(this.resizeTimer);
      this.resizeTimer = setTimeout(() => {
        if (this.slider.isInTransition) {
          this._onScrollEnd();
        } else {
          if (this.autoPlay) {
            this._play();
          }
        }
      this._setSliderWidth(true);
      this.slider.refresh();
      }, 60)
    })
  },
  // keep-alive钩子函数
  activated() {
    if (this.autoPlay) {
      this._play();
    }
  },
  deactivated() {
    clearTimeout(this.timer);
  },
  beforeDestroy() {
    clearTimeout(this.timer);
  },

  methods: {
    //初始化
    init(){
      setTimeout(()=>{
        this._setSliderWidth();
        this._initDots();
        this._initSlider();

        //循环轮播
        if(this.autoPlay){
          this._play();
        }
      },20)
    },

    //轮播图宽度计算
    _setSliderWidth(isResize) {
      this.children = this.$refs.sliderGroup.children; //所有轮播图片
      let width = 0;
      let sliderWidth = this.$refs.slider.clientWidth; //视口宽度
      for (let i = 0; i < this.children.length; i++) {
        let child = this.children[i];
        addClass(child, 'slider-item');
        child.style.width = sliderWidth + 'px'; //单张图片的宽度
        width += sliderWidth;
      }
      if(this.loop && !isResize){
        width += 2*sliderWidth;
      }
      this.$refs.sliderGroup.style.width = width + 'px';
    },

    //小圆点
    _initDots(){
      this.dots = new Array(this.children.length);
    },

    //初始化轮播图
    _initSlider(){
      this.slider = new BScroll(this.$refs.slider,{
        click: this.click,
        scrollX: true,
        scrollY: false,
        momentum: false,
        snap: {
          loop: this.loop,
          threshold: this.threshold,
          speed: this.speed
        },
        bounce: false,
        stopPropagation: false
      })
      //better-scroll自带的滚动钩子函数监听滚动结束
      this.slider.on('scrollEnd',this._onScrollEnd);

      //手动滑动前，清除自动轮播的timer
      this.slider.on('beforeScrollStart',()=>{
        if(this.autoPlay){
          clearTimeout(this.timer);
        }
      })
    },

    //开始轮播
    _play(){
      clearTimeout(this.timer);
      this.timer = setTimeout(()=>{
        this.slider.next(); //better-scroll自带
      },this.interval)
    },
    
    //滚动结束
    _onScrollEnd(){
      //图片滚动后改变对应小圆点样式
      let pageIndex = this.slider.getCurrentPage().pageX;
      this.currentPageIndex = pageIndex;

      //自动轮播
      if(this.autoPlay){
        this._play();
      }
    }
  }
};
</script>

<style scoped lang="stylus">
@import '~common/stylus/variable';

.slider
  min-height: 1px
  .slider-group 
    position: relative
    overflow: hidden
    white-space: nowrap
    .slider-item 
      float: left
      box-sizing: border-box
      overflow: hidden
      a
        display:block
        width: 100%
        overflow: hidden
      img
        display: block
        width: 100%
  .dots
    position: absolute
    right: 0
    left: 0
    bottom: 12px
    text-align: center
    font-size: 0
    .dot
      display: inline-block
      margin: 0 4px
      width: 8px
      height: 8px
      border-radius: 50%
      background: $color-text-l
      &.active
        width: 20px
        border-radius: 5px
        background: $color-theme

</style>